<?php
/**
 * app登录模块
 */
namespace Api\Controller;
use Think\Cache\Driver\Redis;
require_once '/workerman/GatewayClient/Gateway.php';
use GatewayClient\Gateway;
class LoginController extends CommonController {
    //    app登录
	public function index(){
		$username = $_POST['username'];
		$password = $_POST['password'];


		if(!trim($username)){
		    return api_error("用户名不能为空");
		}
		
		if(!trim($password)){
		    return	api_error("密码不能为空");
		}
		//	校验ok  	数据操作
		$ret = D('Admin')->getAdminByUsername($username);
		if(!$ret){
            return api_error('用户不存在');
		}
		if($ret['password']!=getMd5Password($password)){
			return api_error('密码不正确');
		}
        unset($_POST['password']);
		unset($ret['password']);
        $redis = new Redis();

        $userId = $ret['admin_id'];
        $token = $this->getToken($_POST);

        $redis->set( "login_token" . $token , $userId);
        $ret['token'] = $token;
		return api_success("登录成功", $ret);
	}

    // 	扫码事件
    public function scan () {
        $data = $_POST;
        $new_message = array(
            'type'=>'scan',
            'from_client_id'=>$data['uid'],
            'from_client_name' =>$data['uid'],
            'to_client_id'=>$data['uid'],
            'username'=>$_POST['username'],
            'head_img'=>$_POST['head_img'],
            'realname'=>$_POST['realname'],
            'content'=>"扫码成功！请在APP确认登录！",
            'time'=>date('Y-m-d H:i:s'),
        );

// 		推送扫码成功
        Gateway::$registerAddress = C('REGISTER_ADDRESS');
        Gateway::sendToUid($new_message['to_client_id'], json_encode($new_message));
        return api_success('扫码成功',$data['uid']);
    }

    // 		扫码登录接口
    public function login () {

        //		取消登录
        if($_POST['options']== -1){
            $new_message = array(
                'type'=>'login_cancel',
                'from_client_id'=>$_POST['uid'],
                'from_client_name' =>$_POST['uid'],
                'to_client_id'=>$_POST['uid'],
                'username'=>$_POST['username'],
                'content'=>"用户取消授权！",
                'time'=>date('Y-m-d H:i:s'),
            );
            Gateway::$registerAddress = C('REGISTER_ADDRESS');
            $client_id = Gateway::getClientIdByUid($_POST['uid']);
            Gateway::sendToUid($_POST['uid'], json_encode($new_message));
            return api_error('取消', $client_id);
        }

        //		根据token从redis中查找用户身份
        $redis = new Redis();
        $userToken = $_POST['token'];
        if(!$userToken){
            return api_error('没有令牌！');
        }
        $userId = $redis->get("login_token" . $userToken);
        if(!$userId){
            return api_error('令牌非法或过期！' );
        }
        $ret = D('Admin')->getUserById($userId);
        if(!$ret){
            return api_error('抱歉，查无此人！');
        }

        //		对指定客户端链接设置登录身份  uid即为客户端链接的sessionid由二维码传给APP端，APP再请求到这里。
        if($_POST['uid']){
            session_id($_POST['uid']);
            session_start();
            session('adminUser',$ret);
//            return show(0,$ret);
        }

        //		推送到客户端登录成功
        $new_message = array(
            'type'=>'login_success',
            'from_client_id'=>$_POST['uid'],
            'from_client_name' =>$_POST['uid'],
            'to_client_id'=>$_POST['uid'],
            'content'=>"APP已确认，登录成功！",
            'time'=>date('Y-m-d H:i:s'),
        );

        Gateway::$registerAddress = C('REGISTER_ADDRESS');
        $client_id = Gateway::getClientIdByUid($_POST['uid']);
        Gateway::sendToUid($_POST['uid'], json_encode($new_message));
        //        响应APP登录端请求结果
        return api_success('登录成功！',$ret);
    }



}